package com.spring.security.social.qq.config;

import com.spring.security.properties.SecurityProperties;
import com.spring.security.social.HkConnectView;
import com.spring.security.social.qq.connet.QQConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.social.config.annotation.ConnectionFactoryConfigurer;
import org.springframework.social.config.annotation.SocialConfigurerAdapter;

@Configuration
// 当配置了appId的时候才启用
@ConditionalOnProperty(prefix = "hk.security.social.qq", name = "appId")
public class QQAutoConfig extends SocialConfigurerAdapter {
    @Autowired
    private SecurityProperties securityProperties;


    // 添加关于ProviderId=qq的处理器。注意clientId和clientSecret需要自己去qq互联申请
    @Override
    public void addConnectionFactories(
            ConnectionFactoryConfigurer connectionFactoryConfigurer, Environment environment) {
        connectionFactoryConfigurer.addConnectionFactory(
                new QQConnectionFactory(securityProperties.getSocial().getQq().getProviderId(), securityProperties.getSocial().getQq().getAppId(), securityProperties.getSocial().getQq().getAppSecret()));
    }

    /**
     * QQ绑定解绑界面视图
     * connect/qqConnected:绑定
     * connect/qqConnect:解绑
     *
     * @return
     */
    @Bean({"connect/qqConnect", "connect/qqConnected"})
    @ConditionalOnMissingBean(name = "qqConnectedView") //可以自定义实现覆盖此默认界面
    public HkConnectView qqConnectedView() {
        return new HkConnectView();
    }

}
